// FrogRenameSameNameDialog.cpp : Implementation of CFrogRenameSameNameDialog

#include "stdafx.h"
#include "FrogRenameSameNameDialog.h"
#include "..\FilerFrogUtils\FilerFrogUtils.h"

// CFrogRenameSameNameDialog
CFrogRenameSameNameDialog::CFrogRenameSameNameDialog(HICON hIcon):
CFrogProgressReportDialog(hIcon),
m_AnchorIdx(0)
{
}


LRESULT CFrogRenameSameNameDialog::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	CFrogBaseDialog::OnInitDialog(uMsg, wParam, lParam, bHandled);

	HWND editBoxWindowHandle = GetDlgItem(IDC_COMBO_FILES);

	if (editBoxWindowHandle != NULL)
	{
		for (list<wstring>::iterator iter = m_FilesList.begin(); iter != m_FilesList.end(); iter++)
		{
			wstring filePath = FilerFrogUtils::GetFileNameFromPath(*iter);
			SendMessage(editBoxWindowHandle,(UINT)CB_ADDSTRING, 0, (LPARAM)filePath.c_str());
		}

		::SendMessage(editBoxWindowHandle,(UINT)CB_SETCURSEL, (WPARAM) 0 , 0);

		::SetFocus(editBoxWindowHandle);
	}

	return 0;
}

LRESULT CFrogRenameSameNameDialog::OnClickedOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	bHandled = TRUE;
	// TODO: Add your control notification handler code here

	m_AnchorIdx = 0;

	HWND editBoxWindowHandle = GetDlgItem(IDC_COMBO_FILES);

	if (editBoxWindowHandle != NULL)
	{
		LRESULT lResult = ::SendMessage(editBoxWindowHandle,(UINT)CB_GETCURSEL, 0 , 0);

		if (lResult != CB_ERR)
		{
			m_AnchorIdx = (int)lResult;		
		}
	}

	RunAsyncCommandWithProgressBar();

	EndDialog(wID);
	return 0;
}

void CFrogRenameSameNameDialog::ExecuteCommand()
{
	wstring anchor = L"";

	int i = 0;

	for (list<wstring>::iterator iter = m_FilesList.begin(); iter != m_FilesList.end(); iter++)
	{
		if (i == m_AnchorIdx)
		{
			anchor = *iter;
			m_FilesList.remove(*iter);
			break;
		}

		i++;
	}

	if (anchor != L"")
	{
		m_FilesRefactor.RenameFilesMakeSameName(m_LastResult, m_FilesList, anchor);	
	}
}